#include "Card.h"
#include <Windows.h>
#include <iostream>

//Initializes the card, nothing special over here.
void Initialize(card &c, char suit, int rank, bool isFaceUp)
{
	c.suit = suit;
	c.rank = rank;
	c.isFaceUp = isFaceUp;
}

//The main function that displays a card on the screen.
//Everything is done at the specified xPos and yPos location.
//It'll start by displaying the borders, with variation based on if the card is empty or not.
//It'll then fill the card, with variations based on if the card is face up or not.
//It'll eventually decide upon a short version of the card for the game column's visual stacking.
//Otherwise the card is displayed normally using it's c.suit and c.rank values.
void Display(const card &c, int xPos, int yPos, bool isEmptyCard, bool isShortVersion)
{

	DisplayCardBorder(xPos, yPos, isEmptyCard);
	FillCard(c, xPos, yPos, isEmptyCard);
	if (!isEmptyCard && c.isFaceUp)
	{
		if (isShortVersion)
		{
			PlaceChar(char(RANK_SYMBOL[c.rank]), xPos + 1, yPos);//-RANK-
			PlaceChar(char(c.suit), xPos + 3, yPos);//-SUIT-
			PlaceChar(char(c.suit), xPos + 1, yPos+1);//-SUIT-
		}
		else
		{
			PlaceChar(char(RANK_SYMBOL[c.rank]), xPos + 1, yPos + 1);//-RANK-
			PlaceChar(char(c.suit), xPos + 2, yPos + 2);//-SUIT-
			PlaceChar(char(RANK_SYMBOL[c.rank]), xPos + 3, yPos + 3);//-RANK-
		}

	}
}

//Fills the inner part of a card with characters, depending on the card's face-up status.
//Black is used for the front of a card, White is used to display the back of a card.
void FillCard(const card& c, int xPos, int yPos, bool isEmptyCard)
{
	char filler;
	filler = FILLER_BACK;
	if (c.isFaceUp || isEmptyCard)
	{
		filler = FILLER_FRONT;
	}

	for (int x = xPos+1; x <= CARD_INNER_WIDTH + xPos; x++)
	{
		for (int y = yPos+1; y <= CARD_INNER_HEIGHT+ yPos; y++)
		{
			PlaceChar(filler, x, y);
		}
	}
}


//Pretty straightforward, displays the card's border, that is diagonals AND top/bottom/left/right lines.
//If it's an "empty card", there'll be some sexy stuff done aka missing top/bottom/left/right borders.
void DisplayCardBorder(int xPos, int yPos, bool isEmptyCard)
{
	//Ze 4 corners of the card
	PlaceChar(char(218), xPos, yPos); // TopLeft 
	PlaceChar(char(191), xPos + 4, yPos); // TopRight
	PlaceChar(char(192), xPos, yPos + 4); // BottomLeft
	PlaceChar(char(217), xPos + 4, yPos + 4); // BottomRight

		for (int i = 1; i < 4; i++)
		{
			if (!isEmptyCard)
			{
				PlaceChar(char(196), xPos + i, yPos);//-T-
				PlaceChar(char(196), xPos + i, yPos + 4);//-B-
			}
			else
			{
				PlaceChar(char(0), xPos + i, yPos);//-T-
				PlaceChar(char(0), xPos + i, yPos + 4);//-B-
			}
		}
		for (int j = 1; j < 4; j++)
		{
			if (!isEmptyCard)
			{
				PlaceChar(char(179), xPos, yPos + j);//-L-
				PlaceChar(char(179), xPos + 4, yPos + j);//-R-
			}
			else
			{
				PlaceChar(char(0), xPos, yPos + j);//-L-
				PlaceChar(char(0), xPos + 4, yPos + j);//-R-
			}
		}

}

//Straightforward, set the cursor at x,y position, the ugly handle stuff was put in here
void SetCursorPosition(int x, int y)
{
	HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos = { x, y };
	SetConsoleCursorPosition(output, pos);
}
//Places a char at specified position
void PlaceChar(const char& ch, int xPos, int yPos)
{
	SetCursorPosition(xPos, yPos);
	std::cout << ch;
}

void Flip(card& c, bool flipUp)
{
	c.isFaceUp = flipUp;
}